package org.springsecuritylearn.repository.jvm.lession1.demo1;

import java.lang.ref.SoftReference;

/** 弱引用GC测试
 * -Xmx10m
 * @author hyf
 * @Title: ${file_name}
 * @Package org.springsecuritylearn.repository.jvm.lession1.demo1
 * @Description: ${todo}
 * @date 2018/5/3下午11:50
 */
public class SoftRef {

    public static class User{
        public int id;
        public String name;

        public  byte[] bytes;

        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public User(int id, String name, byte[] bytes) {
            this.id = id;
            this.name = name;
            this.bytes = bytes;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        User u=new User(1,"gg",new byte[1024*927*2]);
        SoftReference<User> userSoftReference=new SoftReference<User>(u);
        u=null;
        System.gc();
        System.out.println("1:"+userSoftReference.get());
        System.gc();
        System.out.println("After GC");
        System.out.println("2:"+userSoftReference.get());
        byte[] b=new byte[1024*927*7];
        System.out.println("3:"+userSoftReference.get());
        System.gc();
        System.out.println("4:"+userSoftReference.get());


    }


}
